package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.bizsys.domain.CustReplenishInfo;
import com.ruoyi.bizsys.domain.LoanDeferredOrder;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.MyBeanUtil;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.CustomerInfoMapper;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 客户信息Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    /**
     * 查询客户信息
     *
     * @param id 客户信息ID
     * @return 客户信息
     */
    @Override
    public CustomerInfo selectCustomerInfoById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询客户信息列表
     *
     * @param customerInfo 客户信息
     * @return 客户信息
     */
    @Override
    public List<CustomerInfo> selectCustomerInfoList(CustomerInfo customerInfo) {
        return this.list(new QueryWrapper<>(customerInfo).last("order by id desc"));
    }

    /**
     * 新增客户信息
     *
     * @param customerInfo 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomerInfo(CustomerInfo customerInfo) {
        customerInfo.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(customerInfo);
    }

    /**
     * 修改客户信息
     *
     * @param customerInfo 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomerInfo(CustomerInfo customerInfo) {
        customerInfo.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(customerInfo);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的客户信息ID
     * @return 结果
     */
    @Override
    public int deleteCustomerInfoByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除客户信息信息
     *
     * @param id 客户信息ID
     * @return 结果
     */
    @Override
    public int deleteCustomerInfoById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public String getCustNoByUserId(String userId) {
        CustomerInfo customerInfo = getOne(new QueryWrapper<CustomerInfo>().eq("user_id", userId).eq("state", BizConstants.BIZ_STATUS_1).select("cust_no"));
        if (Func.isNotEmpty(customerInfo)) {
            return customerInfo.getCustNo();
        }
        return null;
    }

    @Override
    public String getUserIdByCustNo(String custNo) {
        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper<CustomerInfo>().eq("cust_no", custNo).select("user_id");
        CustomerInfo one = getOne(queryWrapper);
        return Func.isNotEmpty(one) ? one.getUserId() : null;
    }

    @Override
    public CustomerInfo getOneByCustNo(String custNo) {
        return getOne(new QueryWrapper<CustomerInfo>().eq("cust_no", custNo));
    }

    @Override
    public boolean updateByReplenish(CustomerInfo customerInfo, CustReplenishInfo replenishInfo) {
        replenishInfo.setState(null);
//        BeanUtils.copyProperties(replenishInfo, customerInfo);
        String occupationOther = customerInfo.getOccupationOther();
        String jobTitleOther = customerInfo.getJobTitleOther();
        MyBeanUtil.copyProperties(customerInfo, replenishInfo);
        if (Func.isNotEmpty(customerInfo.getOccupation())) {
            customerInfo.setOccupationOther(occupationOther);
        }
        if (Func.isNotEmpty(customerInfo.getJobTitle())) {
            customerInfo.setJobTitleOther(jobTitleOther);
        }

        return update(customerInfo, new UpdateWrapper<CustomerInfo>().eq("cust_no", replenishInfo.getCustNo()));
    }

    @Override
    public CustomerInfo getOneByUserId(String userId) {
        return getOne(new QueryWrapper<CustomerInfo>().eq("user_id", userId));
    }

    @Override
    public String getUserByCustNo(String custNo) {
        CustomerInfo customerInfo = getOne(new QueryWrapper<CustomerInfo>().eq("cust_no", custNo).select("user_id"));
        return Func.isNotEmpty(customerInfo) ? customerInfo.getCustNo() : null;
    }

    @Override
    public boolean updateByCustNo(CustomerInfo customerInfo) {
        return update(customerInfo, new UpdateWrapper<CustomerInfo>().eq("cust_no", customerInfo.getCustNo()));
    }

    @Override
    public List<ProductInfo> selectDeferred(String custNo, String periodNum) {
        return baseMapper.selectDeferred(custNo,periodNum);
    }


    @Override
    public int upCreditRatingByCustNo(String creditRating, String customerNo) {
        if (Func.hasEmpty(creditRating,customerNo) || "-1".equals(creditRating)){
            return 0;
        }
        return baseMapper.upCreditRatingByCustNo(creditRating,customerNo);
    }

}
